﻿using System.Linq;
using System.Web.Mvc;
using XRM.Security.DataAccess.Enums;
using XRM.Security.DataAccess.Helpers;
using XRM.Security.WebUI.Enums;
using XRM.Security.WebUI.Helpers;
using XRM.Security.WebUI.Models;
using XRM.Security.WebUI.Models.UserRegister;
using XRM.Security.WebUI.Models.UserSkzi;

namespace XRM.Security.WebUI.Controllers
{
    [Authorize]
    public class UserSkziController : BaseController
    {
        [Authorize(Roles = "Administrator, UsersReestrSkziEditor")]
        public ActionResult Index(string text, bool? showAll)
        {
            var skzis = UserSkziRepository.GetAll();
            if (!string.IsNullOrEmpty(text))
                skzis = skzis.Where(x => (x.UserRegister.LastName + " " + x.UserRegister.FirstName + " " + x.UserRegister.MotherName).Replace("ё", "е").Contains(text.Replace("ё", "е")) ||
                    x.CopyNumber.Contains(text) || x.PC.SerialNumber.Contains(text));
            if ((!showAll.HasValue) || (!showAll.Value))
                skzis = skzis.Where(x => !x.DeleteDate.HasValue);
            skzis = skzis.OrderByDescending(x => x.UserRegister.LastName).ThenByDescending(x => x.UserRegister.FirstName).ThenByDescending(x => x.UserRegister.MotherName);
            var model = new UserSkziListModel
            {
                TextFilter = text,
                ShowAllFilter = showAll.HasValue && showAll.Value,
                SkziList = skzis.Select(s => new {s.UserRegister, s.SkziKind.ShortName, s.SetupDate, s.DeleteDate, s.UserSkziId, s.CopyNumber, s.PC.SerialNumber, s.DeleteReason})
                .AsEnumerable().Select(item => new UserSkziModel
                    {
                            UserRegisterFullName = item.UserRegister.FullName(),
                            UserRegisterId = item.UserRegister.UserRegisterId,
                            Id = item.UserSkziId,
                            PCSerialNumber = item.SerialNumber,
                            CopyNumber = item.CopyNumber,
                            DeleteDate = item.DeleteDate,
                            SetupDate = item.SetupDate,
                            SkziKindName = item.ShortName,
                            DeleteReason = item.DeleteReason
                    }).ToList()
            };
            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Administrator, UsersReestrSkziEditor")]
        public ActionResult Filter(UserRegisterListModel model)
        {
            if (!ModelState.IsValid)
            {
                ShowAlert(AlertType.Error, GetModelErrors());
                return View("Index", model);
            }

            return RedirectToAction("Index", new { text = model.TextFilter, showAll = model.ShowAllFilter });
        }

        [HttpGet]
        [Authorize(Roles = "Administrator, UsersReestrSkziEditor")]
        public ActionResult EditDialog(int userSkziId)
        {
            var model = new UserSkziModel(UserSkziRepository.Get(userSkziId));
            model.Init(DataManager);
            return View("~/Views/UserRegister/AddUserSkziDialog.cshtml", model);
        }

        [Authorize( Roles = "Administrator, UsersReestrSkziEditor" )]
        public ActionResult History( int page )
        {
            return History(UserHistoryGroup.Scsi, page);
        }

        [System.Web.Mvc.HttpPost]
        public JsonResult FilterAutocomplete(string term)
        {
            var resultList = UserSkziRepository.GetAll().FilterByFullName(term).ToAutocompleteItems().ToList();

            var searchList2 = UserSkziRepository.GetAll();
            searchList2 = searchList2.Where(x => x.CopyNumber.StartsWith(term));
            resultList.AddRange(searchList2.ToList().Select(item => new AutocompleteItem() { Id = 0, Label = item.CopyNumber, Label2 = "" }).Distinct(new AutocompleteItemComparer()).ToList());

            var searchList3 = UserSkziRepository.GetAll();
            searchList3 = searchList3.Where(x => x.PC.SerialNumber.StartsWith(term.ToLower()));
            resultList.AddRange(searchList3.ToList().Select(item => new AutocompleteItem() { Id = 0, Label = item.PC.SerialNumber, Label2 = "" }).Distinct(new AutocompleteItemComparer()).ToList());

            return Json(resultList, JsonRequestBehavior.AllowGet);
        }
    }
}
